package cn.iotab.iotkit.server.demo.device;

import cn.iotab.iotkit.server.demo.config.TransportAccessProperties;
import cn.iotab.iotkit.server.demo.device.handler.MyDevicePropertyGetHandler;
import cn.iotab.iotkit.server.demo.device.handler.MyDevicePropertyPushHandler;
import cn.iotab.iotkit.server.demo.device.handler.MyDeviceSessionHandler;
import cn.iotab.iotkit.server.demo.device.handler.MyEventAHandler;
import cn.iotab.iotkit.server.demo.device.handler.MyEventBHandler;
import cn.iotab.iotkit.server.demo.device.handler.MyRpcHandler;
import cn.iotab.iotkit.server.demo.device.handler.firmware.MyDeviceReportFirmwareListener;
import cn.iotab.iotkit.server.demo.device.handler.firmware.MyDeviceReportUpgradeProgressListener;
import cn.iotab.iotkit.server.demo.device.handler.firmware.MyDeviceRequestFirmwareListener;
import cn.iotab.iotkit.transport.api.access.device.DeviceManger;
import cn.iotab.iotkit.transport.api.access.device.DeviceOperator;
import cn.iotab.iotkit.transport.api.access.device.HandlerManager;
import cn.iotab.iotkit.transport.api.access.handler.impl.DefaultProductMessageHandler;
import lombok.Getter;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

@Component
public class AppService {

    /**
     * Handler
     */
    @Resource
    private MyDevicePropertyGetHandler myDevicePropertyGetHandler;
    @Resource
    private MyDevicePropertyPushHandler myDevicePropertyPushHandler;
    @Resource
    private MyEventAHandler myEventAHandler;
    @Resource
    private MyEventBHandler myEventBHandler;
    @Resource
    private MyRpcHandler myRpcHandler;
    @Resource
    private MyDeviceSessionHandler sessionHandler;
    @Resource
    private MyDeviceReportFirmwareListener myDeviceReportFirmwareListener;
    @Resource
    private MyDeviceRequestFirmwareListener myDeviceRequestFirmwareListener;
    @Resource
    private MyDeviceReportUpgradeProgressListener myDeviceReportUpgradeProgressListener;


    @Resource
    private TransportAccessProperties transportAccessConfig;

    private DeviceManger deviceManger;

    /**
     * 对设备的操作
     */
    @Getter
    private DeviceOperator deviceOperator;

    public HandlerManager initHandler() {

        DefaultProductMessageHandler handlerGroup = new DefaultProductMessageHandler("*");
        //
        handlerGroup.registerPropGetHandler(myDevicePropertyGetHandler);
        handlerGroup.registerPropPushHandler(myDevicePropertyPushHandler);
        handlerGroup.registerEventHandler("myEventA", myEventAHandler);
        handlerGroup.registerEventHandler("myEventB", myEventBHandler);
        handlerGroup.registerFirmwareReportHandler(myDeviceReportFirmwareListener);
        handlerGroup.registerFirmwareRequestHandler(myDeviceRequestFirmwareListener);
        handlerGroup.registerUpgradeProgressHandler(myDeviceReportUpgradeProgressListener);

        HandlerManager handlerManager = new HandlerManager(myRpcHandler, sessionHandler);
        handlerManager.addDefaultProductProcessor(handlerGroup);
        return handlerManager;
    }

    @PostConstruct
    public void init() {
        HandlerManager handlerManager = initHandler();
        //
        deviceManger = new DeviceManger(transportAccessConfig);
        deviceManger.init(handlerManager);
        //
        deviceOperator = deviceManger.getDeviceOperator();
    }

    @PreDestroy
    public void destroy() {
        if (deviceManger != null){
            deviceManger.destroy();
        }
    }
}
